import { Node } from "cc";
import { app } from "db://app-game/core/app";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { UIID } from "../../common/config/GameUIConfig";
import { LoadDataModelComp } from "./model/LoadDataModelComp";
import { LoadingViewComp } from "./view/LoadingViewComp";

/** 游戏关卡 */
@ecs.register("Load")
export class Load extends ecs.Entity {
  // 数据层
  LoadDataModel!: LoadDataModelComp;
  init() {
    this.add(LoadDataModelComp);
  }
  /** 加载资源 */
  loadRes(
    uiid: UIID,
    loadPaths: { text: string; path: string }[],
    jumpCompleted: (node: Node) => void
  ) {
    // 1.卸载上一个页面资源
    this.unloadRes();

    // 2.加载资源
    this.LoadDataModel.uiid = uiid;
    this.LoadDataModel.loadPaths = loadPaths;
    this.LoadDataModel.jumpCompleted = jumpCompleted;

    app.gui.openAsync(UIID.Loading).then((node) => {
      if (node) {
        this.add(node.getComponent(LoadingViewComp)!);
      }
    });
  }
  /** 卸载资源 */
  unloadRes() {}
}
